//
//  HHNoticeCallContentView.m
//  HHChatKit
//
//  Created by Henry on 2021/9/27.
//

#import "HHNoticeCallContentView.h"
#import <HHTool/HHTool.h>
#import "HHChatConfiguration.h"
#import "HHResources.h"

@interface HHNoticeCallCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation HHNoticeCallCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        self.imageView.backgroundColor = [UIColor redColor];
        CGFloat corner = [HHChatConfiguration shareInstance].defaultAvatarCorner;
        [self.imageView hh_cornerRadius:corner];
        [self.contentView addSubview:self.imageView];
        
    }
    return self;
}

@end


@interface HHNoticeCallContentView () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *joinButton;
@property (nonatomic, strong) UIButton *cancelButton;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HHNoticeCallContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    [self addSubview:self.collectionView];
    [self addSubview:self.cancelButton];
    [self addSubview:self.joinButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, self.frame.size.width, 60);
    self.joinButton.frame = CGRectMake(self.frame.size.width - 150, self.frame.size.height - CGRectGetMaxY(self.collectionView.frame) + 10, 60, 36);
    self.cancelButton.frame = CGRectMake(self.frame.size.width - 80, self.joinButton.frame.origin.y, 60, 36);
}

- (void)relaodData:(NSArray *)data {
    self.dataArray = data.mutableCopy;
    [self.collectionView reloadData];
}

#pragma mark - HHNoticeContentProtocal

- (CGFloat)contentHeight {
    return 116;
}

#pragma mark - UICollectionViewDelegateFlowLayout, UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HHNoticeCallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HHNoticeCallCell" forIndexPath:indexPath];
    return cell;
}

#pragma mark - Event

- (void)onClickCancel {
    if (self.delegate && [self.delegate respondsToSelector:@selector(noticeCallForCancel:)]) {
        [self.delegate noticeCallForCancel:self];
    }
}

- (void)onClickJoin {
    if (self.delegate && [self.delegate respondsToSelector:@selector(noticeCallForJoin:)]) {
        [self.delegate noticeCallForJoin:self];
    }
}

#pragma mark - Getter

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        flowLayout.itemSize = CGSizeMake(40, 40);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
        _collectionView.backgroundColor = self.backgroundColor;
        
        [_collectionView registerClass:[HHNoticeCallCell class] forCellWithReuseIdentifier:@"HHNoticeCallCell"];

    }
    return _collectionView;
}

- (UIButton *)joinButton {
    if (!_joinButton) {
        _joinButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_joinButton setTitle:GetHCLocalizableString(@"Join") forState:UIControlStateNormal];
        [_joinButton addTarget:self action:@selector(onClickJoin) forControlEvents:UIControlEventTouchUpInside];
        _joinButton.backgroundColor = [UIColor greenColor];
        [_joinButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _joinButton.layer.cornerRadius = 5;
    }
    return _joinButton;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_cancelButton setTitle:GetHCLocalizableString(@"Cancel") forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(onClickCancel) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton.backgroundColor = [UIColor grayColor];
        [_cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _cancelButton.layer.cornerRadius = 5;
    }
    return _cancelButton;
}

@end
